package com.ys.peony.model;

import com.ys.peony.app.HttpURL;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseModel;
import com.ys.peony.base.contract.LoginContract;
import com.ys.peony.http.JCHttpManagerUtils;
import com.ys.peony.http.base.BaseRespCallBack;
import com.ys.peony.utils.LogUtil;
import com.ys.peony.utils.SharePreferencesUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 59153 on 2017/7/27.
 */

    public class LoginModel extends BaseModel implements LoginContract.ILoginModel{
    private final static String TAG =  "LoginModel";
    LoginContract.ILoginPresenter loginPresenter;
    @Override
    public void toLogin(String userName, final String password, LoginContract.ILoginPresenter iLoginPresenter) {
        loginPresenter = iLoginPresenter;
        final JCHttpManagerUtils httpManagerUtils = new JCHttpManagerUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("username", userName);
        params.put("password", password);
        httpManagerUtils.get(HttpURL.login, params, new BaseRespCallBack(null) {
            public void onRespSuccess(String respStr) {
                LogUtil.i(TAG, "_login_respStr_" + respStr);
                SharePreferencesUtils.getInstance().setKeyValue(YSConstant.SP_PWD, password);
                loginPresenter.onSuccess(respStr);

            }
            @Override
            public void onRespFailure(String msg) {
                super.onRespFailure(msg);
                loginPresenter.onFail(msg);
            }
        });
    }
}
